package com.fzn3.day03;

import org.junit.Test;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;

import java.util.Arrays;

/**
 * @author fzn
 * 描述：
 * @program DataStructure
 * @create 2023/3/9 9:54
 **/
public class TestSinglyLinkedList {

    @Test
    @DisplayName("遍历")
    public void test1() {
        SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
        singlyLinkedList.addFirst(1);
        singlyLinkedList.addFirst(2);
        singlyLinkedList.addFirst(3);
        singlyLinkedList.addFirst(4);

//        singlyLinkedList.loop1();
//        singlyLinkedList.loop2(value->{
//            System.out.print(value+" ");
//        });
        singlyLinkedList.loop3(value -> {
            System.out.print(value + " ");
        });
    }

    @Test
    @DisplayName("增强for")
    public void test2() {
        SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
        singlyLinkedList.addFirst(1);
        singlyLinkedList.addFirst(2);
        singlyLinkedList.addFirst(3);
        singlyLinkedList.addFirst(4);
        for (Integer value : singlyLinkedList) {
            System.out.print(value + " ");
        }
        System.out.println();
    }

    @Test
    @DisplayName("addLast")
    public void test3() {
        SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
        singlyLinkedList.addLast(1);
        singlyLinkedList.addLast(2);
        singlyLinkedList.addLast(3);
        singlyLinkedList.addLast(4);
        Assertions.assertIterableEquals(Arrays.asList(1, 2, 3, 4), singlyLinkedList);
    }

    @Test
    @DisplayName("get")
    public void test4() {
        SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
        singlyLinkedList.addLast(1);
        singlyLinkedList.addLast(2);
        singlyLinkedList.addLast(3);
        singlyLinkedList.addLast(4);
//        System.out.println(singlyLinkedList.get(2));
        Assertions.assertEquals(2, singlyLinkedList.get(1));
    }

    @Test
    @DisplayName("insert")
    public void test5() {
        SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
        singlyLinkedList.addLast(1);
        singlyLinkedList.addLast(2);
        singlyLinkedList.addLast(3);
        singlyLinkedList.addLast(4);

        singlyLinkedList.insert(0, 5);
        for (Integer value : singlyLinkedList) {
            System.out.print(value + " ");
        }
        System.out.println();
    }

    @Test
    @DisplayName("removeFirst")
    public void test6() {
        SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
        singlyLinkedList.addLast(1);
        singlyLinkedList.addLast(2);

        singlyLinkedList.removeFirst();
        singlyLinkedList.removeFirst();
        for (Integer value : singlyLinkedList) {
            System.out.print(value + " ");
        }
        System.out.println();
        // 不合法
//        singlyLinkedList.removeFirst();
    }

    @Test
    @DisplayName("removeFirst")
    public void test7() {
        SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
        singlyLinkedList.addLast(1);
        singlyLinkedList.addLast(2);
        singlyLinkedList.addLast(3);
        singlyLinkedList.addLast(4);

        singlyLinkedList.remove(3);
        for (Integer value : singlyLinkedList) {
            System.out.print(value + " ");
        }
        System.out.println();
    }

    @Test
    @DisplayName("递归遍历")
    public void test8() {
        SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
        singlyLinkedList.addLast(1);
        singlyLinkedList.addLast(2);
        singlyLinkedList.addLast(3);
        singlyLinkedList.addLast(4);
        singlyLinkedList.loop4();
    }

    @Test
    @DisplayName("递归遍历")
    public void test9() {
        SinglyLinkedList singlyLinkedList = new SinglyLinkedList();
        singlyLinkedList.addLast(1);
        singlyLinkedList.addLast(2);
        singlyLinkedList.addLast(3);
        singlyLinkedList.addLast(4);
        singlyLinkedList.loop5(value -> {
            System.out.println("before:" + value);
        }, value -> {
            System.out.println("after:" + value);
        });
    }

}
